package com.frxuexi.user.view

import android.text.TextUtils
import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.User
import com.frxuexi.user.intent.UserIntent
import com.frxuexi.user.viewmodel.UserViewModel
import kotlinx.coroutines.launch

@Composable
fun RegisterPage (navHostController: NavHostController,vm:UserViewModel= hiltViewModel()){
    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        Log.i("===",it.data.toString())
                        navHostController.popBackStack()
                    }

                    else -> {}
                }
            }
        }
    })
    var uname by remember {
        mutableStateOf("")
    }
    var pass by remember {
        mutableStateOf("666")
    }
    var pass2 by remember {
        mutableStateOf("666")
    }
    Column(verticalArrangement = Arrangement.spacedBy(20.dp), modifier = Modifier.padding(20.dp)) {
        BaseTitle(title = "注册", navHostController = navHostController)
        Spacer(modifier = Modifier.height(100.dp))
        OutlinedTextField(value = uname, onValueChange ={uname=it},Modifier.fillMaxWidth()
            , label = { Text(text = "用户名")}, leadingIcon = { Icon(
                imageVector = Icons.Default.AccountCircle,
                contentDescription = ""
            )}
        )
        OutlinedTextField(value = pass, onValueChange ={pass=it},Modifier.fillMaxWidth() ,
            label = { Text(text = "密码")}, leadingIcon = { Icon(
            imageVector = Icons.Default.Lock,
            contentDescription = ""
        )},visualTransformation = PasswordVisualTransformation() )
        OutlinedTextField(value = pass2, onValueChange ={pass2=it} ,Modifier.fillMaxWidth() ,
            label = { Text(text = "确认密码")}, leadingIcon = { Icon(
                imageVector = Icons.Default.Lock,
                contentDescription = ""
            )},visualTransformation =PasswordVisualTransformation())

        var role by remember {
            mutableStateOf("员工")
        }
        Row(verticalAlignment = Alignment.CenterVertically) {
            Text(text = "角色：")
            Checkbox(checked = role=="经理", onCheckedChange = {
                if(it){
                    role="经理"
                }
            })
            Text(text = "经理")
            Checkbox(checked = role=="员工", onCheckedChange = {
                if(it){
                    role="员工"
                }
            })
            Text(text = "员工")
        }
        Button(modifier = Modifier.fillMaxWidth() ,onClick = {
            if(TextUtils.isEmpty(uname) || TextUtils.isEmpty(pass) || TextUtils.isEmpty(pass2)){
                ToastUtils.showLong("输入不能为空")
                return@Button
            }
//            if(RegexUtils.isUsername())
            if(!uname.matches(Regex("[A-z0-9_]{3,10}"))){
                ToastUtils.showLong("用户名不合法")
                return@Button
            }
            if (pass!=pass2){
                ToastUtils.showLong("两次密码不一致")
                return@Button
            }
            vm.sendIntent(UserIntent.register(User(uname = uname, upass = pass, role = role)))
        }) {
            Text(text = "注册")
        }

    }


}